// HSS.Forms.LabelGradient.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       LabelGradient.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace HSS.Forms
{
	/// <summary>
	/// Summary description for LabelGradient.
	/// </summary>
	public partial class LabelGradient : System.Windows.Forms.UserControl
	{
		/// <summary>
		/// Default Constructor
		/// </summary>
		public LabelGradient()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// Activates double buffering 
			SetStyle(ControlStyles.UserPaint, true);
			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			SetStyle(ControlStyles.DoubleBuffer, true);

			// Forces Refresh on Resize
			SetStyle(ControlStyles.ResizeRedraw, true);
		}

		/// <summary>
		/// The High Color to use when painting the Gradient.
		/// </summary>
		[DefaultValueAttribute(typeof(Color), "Blue")]
		[DescriptionAttribute("Beginning Color")]
		[CategoryAttribute("Appearance")]
		public Color GradientHighColor
		{
			get { return _highColor; }
			set
			{
				_highColor = value;
				this.Refresh();
			}
		} Color _highColor = Color.Blue;

		/// <summary>
		/// The Low Color to use when painting the Gradient.
		/// </summary>
		[DefaultValueAttribute(typeof(Color), "AliceBlue")]
		[DescriptionAttribute("Ending Color")]
		[CategoryAttribute("Appearance")]
		public Color GradientLowColor
		{
			get { return _lowColor; }
			set
			{
				_lowColor = value;
				this.Refresh();
			}
		} Color _lowColor = Color.AliceBlue;

		/// <summary>
		/// Gets/Sets the Gradient Mode.
		/// </summary>
		[DefaultValueAttribute(LinearGradientMode.Horizontal)]
		[DescriptionAttribute("What orientation to draw the gradient.")]
		[CategoryAttribute("Appearance")]
		public LinearGradientMode GradientMode
		{
			get { return _horizontal; }
			set
			{
				_horizontal = value;
				this.Refresh();
			}
		} LinearGradientMode _horizontal = LinearGradientMode.Horizontal;

		/// <summary>
		/// Indicates whether or not use Gamma Correction when paintin the background.
		/// </summary>
		[DefaultValueAttribute(false)]
		[DescriptionAttribute("Draw the gradient using GammaCorrection")]
		[CategoryAttribute("Appearance")]
		public bool UseGammaCorrection
		{
			get { return _useGammaCorrection; }
			set
			{
				_useGammaCorrection = value;
				this.Refresh();
			}
		} bool _useGammaCorrection = false;

		/// <summary>
		/// Indicates whether or not to use Anti-Aliasing. Preferred is no (false).
		/// </summary>
		[DefaultValueAttribute(false)]
		[DescriptionAttribute("Draw using AntiAlias")]
		[CategoryAttribute("Appearance")]
		public bool AntiAlias
		{
			get
			{
				return _antiAlias;
			}

			set
			{
				_antiAlias = value;
				Invalidate();
			}
		} bool _antiAlias = false;

		/// <summary>
		/// The Text Off Set Value
		/// </summary>
		[DefaultValueAttribute(4)]
		[DescriptionAttribute("The number spaces the beginning of Text will be padded")]
		[CategoryAttribute("Appearance")]
		public int TextOffSet
		{
			get { return _offSet; }
			set
			{
				_offSet = value;
				Invalidate();
			}
		} int _offSet = 4;

		/// <summary>
		/// The Text Trimming Format
		/// </summary>
		[DefaultValueAttribute(StringTrimming.EllipsisWord)]
		[DescriptionAttribute("The StringFormat Trimming Option to use.")]
		[CategoryAttribute("Appearance")]
		public StringTrimming TextTrimming
		{
			get { return _stringTrimming; }
			set
			{
				_stringTrimming = value;
				Invalidate();
			}
		} StringTrimming _stringTrimming = StringTrimming.EllipsisWord;

		/// <summary>
		/// The Text Wrapping Format
		/// </summary>
		[DefaultValueAttribute(StringFormatFlags.NoWrap)]
		[DescriptionAttribute("The StringFormat Wrapping Option to use.")]
		[CategoryAttribute("Appearance")]
		public StringFormatFlags TextWrapping
		{
			get { return _textWrapping; }
			set
			{
				_textWrapping = value;
				Invalidate();
			}
		} StringFormatFlags _textWrapping = StringFormatFlags.NoWrap;

		/// <summary>
		/// The Text Alignment of the label
		/// </summary>
		[DefaultValueAttribute(StringAlignment.Center)]
		[DescriptionAttribute("The String Alignment Option to use.")]
		[CategoryAttribute("Appearance")]
		public StringAlignment TextAlignment
		{
			get { return _textAlignment; }
			set
			{
				_textAlignment = value;
				Invalidate();
			}
		} StringAlignment _textAlignment = StringAlignment.Center;

		/// <summary>
		/// The Text for the Label. Shared with the Caption Property
		/// </summary>
		[DescriptionAttribute("Text displayed in the caption.")]
		[CategoryAttribute("Appearance")]
		[Browsable(true)]
		public new string Text
		{
			get { return _caption; }
			set
			{
				_caption = value;
				Invalidate();
			}
		}

		/// <summary>
		/// The caption Text
		/// </summary>
		[Browsable(false)]
		public string Caption
		{
			get { return _caption; }
			set
			{
				_caption = value;
				Invalidate();
			}
		} string _caption;

		/// <summary>
		/// The caption to be drawn
		/// </summary>
		/// <param name="e">PaintEventArgs</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			using (LinearGradientBrush GBrush =
				new LinearGradientBrush(this.DisplayRectangle, _highColor, _lowColor, this.GradientMode))
			{

				GBrush.GammaCorrection = _useGammaCorrection;

				e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
				e.Graphics.InterpolationMode = InterpolationMode.High;

				e.Graphics.FillRectangle(GBrush, this.DisplayRectangle);
			}

			// need a rectangle when want to use ellipsis
			RectangleF bounds = new RectangleF(_offSet,
				0,
				this.DisplayRectangle.Width - _offSet,
				this.DisplayRectangle.Height);

			// format used when drawing the text
			StringFormat format = new StringFormat();
			format.FormatFlags = _textWrapping;
			format.LineAlignment = _textAlignment;
			format.Trimming = TextTrimming;

			if (_antiAlias)
				e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

			e.Graphics.DrawString(this._caption, this.Font, new SolidBrush(this.ForeColor), bounds, format);
			
			format.Dispose();
			
			base.OnPaint(e);
		}

	}
}